<!doctype html>



  


<html class="theme-next pisces use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Hexo, NexT" />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.1" />






<meta name="description" content="title: jsonp详解tags: categories:##ajax不允许跨域获取数据说明，假定后端语言是   PHP script标签jsonp的原理：script标签具有跨域性，可以利用Script标签的src属性发送跨域请求，获取相应数据1234567&amp;lt;script src=&amp;quot;域名/域名下的php文件名?callbackName=fnName&amp;quot;&amp;gt;&amp;lt;">
<meta property="og:type" content="article">
<meta property="og:title" content="西瓜古古🍉">
<meta property="og:url" content="http://yoursite.com/2017/09/24/jsonp/index.html">
<meta property="og:site_name" content="西瓜古古🍉">
<meta property="og:description" content="title: jsonp详解tags: categories:##ajax不允许跨域获取数据说明，假定后端语言是   PHP script标签jsonp的原理：script标签具有跨域性，可以利用Script标签的src属性发送跨域请求，获取相应数据1234567&amp;lt;script src=&amp;quot;域名/域名下的php文件名?callbackName=fnName&amp;quot;&amp;gt;&amp;lt;">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-09-23T16:05:41.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="西瓜古古🍉">
<meta name="twitter:description" content="title: jsonp详解tags: categories:##ajax不允许跨域获取数据说明，假定后端语言是   PHP script标签jsonp的原理：script标签具有跨域性，可以利用Script标签的src属性发送跨域请求，获取相应数据1234567&amp;lt;script src=&amp;quot;域名/域名下的php文件名?callbackName=fnName&amp;quot;&amp;gt;&amp;lt;">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: 0,
      author: '博主'
    }
  };
</script>

  <title>  | 西瓜古古🍉 </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  










  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">西瓜古古🍉</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle"></p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time itemprop="dateCreated" datetime="2017-09-24T00:05:41+08:00" content="2017-09-24">
              2017-09-24
            </time>
          </span>

          

          
            
          

          

          
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>title: jsonp详解<br>tags:</p>
<h2 id="categories"><a href="#categories" class="headerlink" title="categories:"></a>categories:</h2><p>##<br>ajax不允许跨域获取数据<br>说明，假定后端语言是   PHP</p>
<h2 id="script标签"><a href="#script标签" class="headerlink" title="script标签"></a>script标签</h2><p>jsonp的原理：script标签具有跨域性，可以利用Script标签的src属性发送跨域请求，获取相应数据<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">&lt;script src=&quot;域名/域名下的php文件名?callbackName=fnName&quot;&gt;&lt;/script&gt;</div><div class="line">&lt;script&gt;</div><div class="line">    // 回调函数</div><div class="line">    function fnName(data)&#123;</div><div class="line">        console.log(data);</div><div class="line">    &#125;</div><div class="line">&lt;/script&gt;</div></pre></td></tr></table></figure></p>
<p>后端返回的是js代码，是函数调用<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">&lt;?php $callbackName = $_GET[&apos;callback&apos;] </div><div class="line">echo $callbackName.&apos;(hello world)&apos;;</div><div class="line">?&gt;</div></pre></td></tr></table></figure></p>
<h2 id="jQuery-jsonp"><a href="#jQuery-jsonp" class="headerlink" title="jQuery-jsonp"></a>jQuery-jsonp</h2><p>type必须是get<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">$.ajax(&#123;</div><div class="line">    url: &quot;域名/域名下的php文件名&quot;,</div><div class="line">    dataType: &quot;jsonp&quot;,</div><div class="line">    jsonp: &quot;callbackName&quot;,  // 约定等号左边的key，服务器根据该名称获取回调函数名称</div><div class="line">    jsonpCallback: &quot;fnName&quot;, // 约定等号右边的value，标识回调函数名称</div><div class="line">    success: function()&#123;</div><div class="line">        // todo</div><div class="line">    &#125;,</div><div class="line">    error: function()&#123;</div><div class="line">        // todo</div><div class="line">    &#125;</div><div class="line">&#125;)</div></pre></td></tr></table></figure></p>
<h2 id="模仿封装jQuery-ajax"><a href="#模仿封装jQuery-ajax" class="headerlink" title="模仿封装jQuery-ajax"></a>模仿封装jQuery-ajax</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">myAjax</span>(<span class="params">options</span>) </span>&#123;</div><div class="line">  <span class="comment">// 默认值</span></div><div class="line">  <span class="keyword">var</span> defaults = &#123;</div><div class="line">      jsonp: <span class="string">"callback"</span>,</div><div class="line">      jsopCallback: <span class="string">"jQuery"</span>+ (<span class="string">'v3.1.1'</span>+ <span class="built_in">Math</span>.random()).replace(<span class="regexp">/\D/g</span>,<span class="string">''</span>)+<span class="string">"_"</span>+<span class="keyword">new</span> <span class="built_in">Date</span>().getTime();</div><div class="line">  &#125;;</div><div class="line">  <span class="comment">// window是全局对象，[]中的值可以是变量，也可以是字符串</span></div><div class="line">  <span class="built_in">window</span>[defaults.jsopCallback] = <span class="function"><span class="keyword">function</span>(<span class="params">data</span>)</span>&#123;</div><div class="line">      defaults.success(data);</div><div class="line">  &#125;;</div><div class="line">  <span class="comment">// 处理业务参数,把data数据转换为查询字符串</span></div><div class="line">  <span class="keyword">var</span> param = <span class="string">''</span>;</div><div class="line">  <span class="keyword">if</span>(defaults.data)&#123;</div><div class="line">      <span class="keyword">for</span>(<span class="keyword">var</span> key <span class="keyword">in</span> defaults.data)&#123;</div><div class="line">          param += key+<span class="string">"="</span>+defaults.data[key]+<span class="string">"&amp;"</span>;</div><div class="line">      &#125;</div><div class="line">  &#125;</div><div class="line">  </div><div class="line">  <span class="comment">// 使用设置参数覆盖默认配置</span></div><div class="line">  <span class="keyword">for</span>(<span class="keyword">var</span> key <span class="keyword">in</span> options)&#123;</div><div class="line">      defaults[key] = options[key];</div><div class="line">  &#125;;</div><div class="line">  <span class="comment">// 处理跨域请求</span></div><div class="line">  <span class="keyword">var</span> script = <span class="built_in">document</span>.createElement(<span class="string">"script"</span>);</div><div class="line">  script.src = defaults.url+<span class="string">"?"</span>+ param + defaults.jsonp+<span class="string">"="</span>+defaults.jsonpCallback;</div><div class="line">  <span class="keyword">var</span> head = <span class="built_in">document</span>.querySelector(<span class="string">"head"</span>);</div><div class="line">  head.appendChild(script);</div><div class="line">  &#125;</div></pre></td></tr></table></figure>

      
    </div>

    <div>
      
        
      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/09/23/jQuery插件-validate/" rel="next" title="Query插件-validate">
                <i class="fa fa-chevron-left"></i> Query插件-validate
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/09/24/富文本插件/" rel="prev" title="富文本插件">
                富文本插件 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/23103K3MK.jpg"
               alt="beikexixi" />
          <p class="site-author-name" itemprop="name">beikexixi</p>
          <p class="site-description motion-element" itemprop="description"></p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">35</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">9</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">10</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com" target="_blank" title="Github">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  Github
                </a>
              </span>
            
          
        </div>

        
        

        
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#categories"><span class="nav-number">1.</span> <span class="nav-text">categories:</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#script标签"><span class="nav-number">2.</span> <span class="nav-text">script标签</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#jQuery-jsonp"><span class="nav-number">3.</span> <span class="nav-text">jQuery-jsonp</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#模仿封装jQuery-ajax"><span class="nav-number">4.</span> <span class="nav-text">模仿封装jQuery-ajax</span></a></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">beikexixi</span>
</div>

<div class="powered-by">
  由 <a class="theme-link" href="http://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>

        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/vendors/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/vendors/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/vendors/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.1"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.0.1"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.1"></script>



  



  



  
  
  

  

  

</body>
</html>
